import java.util.Random;
import java.util.Vector;

//handles animal reproduction
//calls birthhandler
//sets parent flag
//makes call to system time handlers


//mate handler adjusts animal obj
//need to create heirarchy of animals based on dominance
//add an animal to the AnimalHandler
public class MateHandler {
	public Vector<AnimalObject> potentials = new Vector<AnimalObject>();

	public static boolean DEBUGA = false;
	public static void setDEBUGA(boolean b) {DEBUGA = b;}	
	public static boolean DEBUGB = false;
	public static void setDEBUGB(boolean b) {DEBUGB = b;}
	public static boolean DEBUGC = false;
	public static void setDEBUGC(boolean b) {DEBUGC = b;}

	public static Vector<loc> shuffle(Vector<loc> old)
	{
		Random R = new Random();
		Vector<loc> N = new Vector<loc>();
		int S = old.size();
		for(int i=0; i<S;i++)
		{
			int rand = R.nextInt(old.size());
			N.add(old.get(rand));
			old.remove(rand);
		}
		return N;
	}

	public static void findPotentials(AnimalObject me) {
		Vector<loc> bleh = me.getMyAreaLocs();
		bleh = shuffle(bleh); //get to looking
		Vector<AnimalObject> bootycall = new Vector<AnimalObject>();
		Random R = new Random();
		if(DEBUGA)
			System.out.println(me.getType()+","+me.getSex());
		for(int i = 0; i < bleh.size(); i++) {
			for(int j = 0; j < AnimalHandlerObject.getAnimalVector().size(); j++) {
				if( (AnimalHandlerObject.getAnimalVector().get(j).getLocation().x == bleh.get(i).x) 
						&&  (AnimalHandlerObject.getAnimalVector().get(j).getLocation().y == bleh.get(i).y) ) {
					//if there is an animal in the animal handler with the location we are polling
					if(AnimalHandlerObject.getAnimalVector().get(j).getType() == me.getType()) {
						//check its type
						if(!AnimalHandlerObject.getAnimalVector().get(j).getSex()) {
							//check its sex
							if(AnimalHandlerObject.getAnimalVector().get(j).getMateable() <= 0)
								bootycall.add(AnimalHandlerObject.getAnimalVector().get(j));
							if(DEBUGB)
								System.out.println("WE GOTS A PIECE O BOOTy!");
							//add it to our list of potential bootycalls if its not weening
						}
					}
				}
			}
		}
		if(bootycall.size() == 0) {
			me.setMigrate(true);
			me.setDisposition(0);
			if(DEBUGB)
				System.out.println("we need to find a new track to plow"); //test
		} else {
			//im going to cheat here and just move the stupid animal
			if(DEBUGB)
				System.out.println("handing off to spawning");
			Mate(me, bootycall.get(R.nextInt(bootycall.size())));

		}

	}


	public static void Mate(AnimalObject me, AnimalObject mybitch) {
		mybitch.setMateable(100); //we is preggers!
		me.setDisposition(2); //i is hungry... munchies!
		Random R = new Random();
		int b = 1 + R.nextInt(5);
		for(int i = 0; i < b; i++) {
			if(DEBUGC)
				System.out.println("Spawning "+i+"animals");
			BirthHandler.doBirthingSpawn(me, mybitch);
		}
	}


}